﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;

namespace WorkStation.Frameworks.ServiceContainer.Unity
{
    public class UnityContainerProvider : AbstractServiceContainerProvider
    {
        #region IServiceContainerProvider 成员

        public override IServiceLocator GetServiceContainer()
        {
            UnityContainer unityContainer = new UnityContainer();

            foreach (var itemConfig in this.Config.ItemConfigs.Configs)
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, itemConfig.FilePath);

                var unitySection = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None).GetSection("unity") as Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;

                //var unitySection = new FileConfigurationSource(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, itemConfig.FilePath)).GetSection("unity") as UnityConfigurationSection;

                unitySection.Configure(unityContainer);
            }

            UnityServiceLocator locator = new UnityServiceLocator(unityContainer);

            return locator;
        }

        #endregion IServiceContainerProvider 成员
    }
}
